package com.greateffect.littlebud.lib.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;

import com.greateffect.littlebud.lib.R;
import com.greateffect.littlebud.lib.mvp.IPresenter;
import com.greateffect.littlebud.lib.mvp.IViewAdvance;
import com.greateffect.littlebud.lib.provider.CustomStatusPageHelper;
import com.greateffect.littlebud.lib.utils.JColors;

/**
 * Created by ZengCS on 2019/1/8.
 * E-mail:zengcs@vip.qq.com
 * Add:成都市天府软件园E3
 */
public abstract class BaseSwipeActivityAdv<P extends IPresenter> extends BaseNormalActivityAdv<P> implements IViewAdvance, SwipeRefreshLayout.OnRefreshListener {
    protected int currPage = 1;
    protected boolean hasMoreData = false;
    protected View notDataView, errorView, mLoadingView;
    protected SwipeRefreshLayout mSwipeRefreshLayout;

    @Nullable
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initStatusPages();
    }

    /**
     * 初始化状态页面
     */
    protected void initStatusPages() {
        if (notDataView == null) {
            notDataView = CustomStatusPageHelper.makeEmptyView(0, "");
        }
        if (notDataView != null) notDataView.setOnClickListener(v -> onRefresh());
        if (errorView == null) {
            errorView = CustomStatusPageHelper.makeErrorView(0, "");
        }
        if (errorView != null) errorView.setOnClickListener(v -> onRefresh());
        if (mLoadingView == null) {
            mLoadingView = CustomStatusPageHelper.makeLoadingView(getString(R.string.txt_loading_common));
        }
    }

    protected void addSwipeRefreshAbility() {
        mSwipeRefreshLayout = findViewById(R.id.swipe_refresh_layout_common);
        if (mSwipeRefreshLayout == null) return;
        mSwipeRefreshLayout.setColorSchemeColors(JColors.colorPrimary, JColors.red, JColors.blueLight);
        mSwipeRefreshLayout.setOnRefreshListener(this);
        // 默认不可用，第一次加载成功后才能用
        mSwipeRefreshLayout.setEnabled(false);
    }

    @Override
    public void hideLoading() {
        super.hideLoading();
        if (mSwipeRefreshLayout != null) {
            mSwipeRefreshLayout.setEnabled(true);
            mSwipeRefreshLayout.setRefreshing(false);
        }
    }

    /**
     * 下拉刷新和加载更多时，不会显示Loading
     */
    @Override
    public void showLoading(String msg) {
        isLoading = true;
        // 如果是下拉刷新，不显示Loading框
        if (mSwipeRefreshLayout != null && mSwipeRefreshLayout.isRefreshing()) return;
        // 如果是加载更多，也不显示Loading框
        if (currPage > 1) return;

        // 显示Loading框
        super.showLoading(msg);
    }
}
